package cn.lutu.adapter.controller;

import cn.lutu.domain.service.AdminService;
import cn.lutu.protocol.dto.AdminSelectUserDTO;
import cn.lutu.protocol.dto.AuthorityUpdateDTO;
import cn.lutu.protocol.dto.RoleAddDTO;
import cn.lutu.protocol.dto.RoleUpdateInfoDTO;
import cn.lutu.protocol.vo.AuthorityVO;
import cn.lutu.protocol.vo.adminVO.*;
import cn.lutu.util.response.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "管理员模块")
@RestController
@PreAuthorize("hasAuthority('/admin')")
@RequestMapping("/v1/admins/")
public class AdminController {
    @Autowired
    AdminService adminService;

    @GetMapping("userList")
    @ApiOperation("查询用户列表")
    public ResultVO userList(){
        log.info("开始处理查询用户列表业务");
        List<AdminUserListVO> adminUserListVOS = adminService.selectUserList();
        log.info("查询结束，查询到的结果为：{}",adminUserListVOS);
        return ResultVO.ok(adminUserListVOS);
    }
    @GetMapping("roleList")
    @ApiOperation("查询角色列表")
    public ResultVO roleList(){
        log.info("开始处理查询角色列表业务");
        List<AdminRoleListVO> list = adminService.roleList();
        log.info("数据为：{}",list);
        return ResultVO.ok(list);
    }

    @GetMapping("authorityList")
    @ApiOperation("查询角色列表")
    public ResultVO authorityList(){
        log.info("开始处理查询角色列表业务");
        List<AuthorityVO> list = adminService.authList();
        log.info("数据为：{}",list);
        return ResultVO.ok(list);
    }
    @GetMapping("spotList")
    @ApiOperation("查询景点列表")
    public ResultVO spotList(){
        log.info("开始处理查询景点列表业务");
        List<AdminSpotVO> list = adminService.spotList();
        log.info("数据为：{}",list);
        return ResultVO.ok(list);
    }
    @GetMapping("roleAuthorityList")
    @ApiOperation("查询权限列表")
    public ResultVO roleAuthorityList(){
        log.info("开始处理查询权限列表业务");
        List<AuthorityUpdateVO> list = adminService.authorityList();
        log.info("数据为：{}",list);
        return ResultVO.ok(list);
    }
    @PostMapping("selectByUser")
    @ApiOperation("根据条件查询用户列表")
    public ResultVO adminSelectUser(AdminSelectUserDTO adminSelectUserDTO){
        log.info("开始处理管理员根据User查询数据，参数User为：{}",adminSelectUserDTO);
        return ResultVO.ok(adminService.userListBy(adminSelectUserDTO));
    }
    @GetMapping("/user/{userName}/detail")
    @ApiOperation("根据用户名查询用户详情")
    public ResultVO UserDetail(@PathVariable String userName){
        log.info("开始处理管理员根据用户名查询用户详情数据，参数为：{}",userName);
        return ResultVO.ok(adminService.getUserInfoByUsername(userName));
    }
    @GetMapping("/{id:[0-9]+}")
    public ResultVO getInfoById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询角色数据】的请求，参数：{}", id);
        RoleUpdateInfoVO roleUpdateInfoVO = adminService.getInfoById(id);
        return ResultVO.ok(roleUpdateInfoVO);
    }
    @GetMapping("/{id:[0-9]+}/spot")
    public ResultVO getSpotById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询景点数据】的请求，参数：{}", id);
        AdminSpotVO adminSpotVO = adminService.getSpotById(id);
        return ResultVO.ok(adminSpotVO);
    }

    @GetMapping("/{id:[0-9]+}/authority")
    public ResultVO getAuthorityInfoById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询权限数据】的请求，参数：{}", id);
        AuthorityUpdateVO authorityUpdateVO = adminService.getAuthority(id);
        return ResultVO.ok(authorityUpdateVO);
    }
    @PostMapping("/{id}/delete")
    @ApiOperation("删除类型")
    public ResultVO deleteById(@PathVariable Long id){
        log.info("开始执行删除类型请求");
        adminService.deleteById(id);
        return ResultVO.ok();
    }
    @PostMapping("/{id}/deleteAuthority")
    @ApiOperation("删除权限")
    public ResultVO deleteAuthorityById(@PathVariable Long id){
        log.info("开始执行删除权限请求");
        adminService.deleteAuthorityById(id);
        return ResultVO.ok();
    }
    @ApiOperation("修改角色")
    @PostMapping("/{id:[0-9]+}/update")
    public ResultVO updateInfoById(@Validated RoleUpdateInfoDTO roleUpdateInfoDTO) {
        log.debug("开始处理【修改用户角色】的请求，参数：{}", roleUpdateInfoDTO);
        adminService.updateInfoById(roleUpdateInfoDTO);
        return ResultVO.ok();
    }
    @PostMapping("/{userName}/add-new")
    @ApiOperation("添加类别")
    public ResultVO addNew(@Valid RoleAddDTO roleAddDTO) {
        log.debug("开始处理【添加角色】的请求，参数：{}", roleAddDTO);
        adminService.addNewRole(roleAddDTO);
        return ResultVO.ok();
    }
    @PostMapping("/{roleId}/{authorityId}/new-add")
    @ApiOperation("添加权限")
    public ResultVO addNewAuthority(@PathVariable Long roleId,@PathVariable Long authorityId) {
        log.debug("开始处理【添加权限】的请求，参数：{},{}",roleId,authorityId );
        adminService.addNewAuthority(roleId,authorityId);
        return ResultVO.ok();
    }
    @ApiOperation("修改权限")
    @PostMapping("/{id:[0-9]+}/updateAuthority")
    public ResultVO updateAuthorityById(@Validated AuthorityUpdateDTO authorityUpdateDTO) {
        log.debug("开始处理【修改权限】的请求，参数：{}", authorityUpdateDTO);
        adminService.updateAuthorityById(authorityUpdateDTO);
        return ResultVO.ok();
    }

}
